package com.yan.deezer.network;

import java.util.List;

import org.apache.http.client.methods.HttpRequestBase;

import com.yan.deezer.data.object.Album;
import com.yan.deezer.data.object.Artist;
import com.yan.deezer.data.object.Track;
import com.yan.deezer.data.parser.ParsingManager;

public class ResponseBuilder {
	private final static String TAG = "REPONSE_BUILDER";

	/*
	 * public static List<Artist> getPopularOrLatestArticles(HttpRequestBase
	 * request) throws Exception{ String response = new
	 * String(getData(request)); Debug.debugTemporary(TAG,
	 * "getPopularOrLatestFilms response = "+response.replace(",", ",\n"));
	 * if(response == null){ return null; } return
	 * ParsingManager.buildFilmsList(response); }
	 */

	public static List<Artist> getArtistList(HttpRequestBase request)
			throws Exception {
		String response = new String(getData(request));
		if (response == null) {
			return null;
		}
		return ParsingManager.buildArtistList(response);
	}

	public static List<Album> getArtistAlbumList(HttpRequestBase request)
			throws Exception {
		String response = new String(getData(request));
		if (response == null) {
			return null;
		}
		return ParsingManager.buildArtistAlbumList(response);
	}

	public static List<Track> getAlbumTrackList(HttpRequestBase request)
			throws Exception {
		String response = new String(getData(request));
		if (response == null) {
			return null;
		}
		return ParsingManager.buildAlbumTrackList(response);
	}

	/*
	 * public static Article getArticleDetails(HttpRequestBase request) throws
	 * Exception{ String response = new String(getData(request));
	 * Debug.debugTemporary(TAG,
	 * "getFilmDetails response = "+response.replace(",", ",\n")); if(response
	 * == null){ Debug.debugWarning(TAG, "getFilmDetails response == null");
	 * return null; } return ParsingManager.buildFilmDetailed(response); }
	 */

	private static byte[] getData(HttpRequestBase httpRequest) throws Exception {
		try {
			HttpResponse response = HttpConnector.getHttpResponse(httpRequest);
			int responseCode = response.getCode();

			if (responseCode == HttpCode.OK) {
				response.readBody();
			} else {
				response.close();
				throw new Exception("HTTP request failed [" + responseCode
						+ "]"/* , CommunicationException.HTTP_ERROR */);
			}
			byte[] data = response.getByteContent();
			response.close();
			return data;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("HTTP request failed"/*
													 * , CommunicationException.
													 * HTTP_ERROR
													 */);
		}
	}

}
